********************************************************************************
* TABLE 1:  VENDOR											 				   * 
********************************************************************************
	use "$temporales\Bcom_976total_2022.dta",clear 

	la define social_reactions 1 "Nothing happens" 2 "Other vendors apply a little pressure" 3 "Other vendors apply a lot of pressure" 4 "The board admonishes the violator" 5 "The board fines the violator" 6 "The board closes the violator's stand temporarily" 7 "The board expels the violator"
	la define vecFIN 0 "No social ties" 1 "Social ties"
	la values indenf_nopaga indenf_estafa indenf_limpia social_reactions
	la values vecFIN vecFIN

	eststo: estpost tab indenf_nopaga vecFIN, nototal
	eststo: estpost tab indenf_estafa vecFIN, nototal
	eststo: estpost tab indenf_limpia vecFIN, nototal

	esttab using "$tablas\tabla1_enforcement.tex", cell(colpct(fmt(2))) collabels(none) unstack noobs modelwidth(15) varlabels(`e(labels)') eqlabels(`e(eqlabels)') nomtitle nonumbers nodepvars replace booktabs mgroups("Non-payment of market assessments" "Swindles other vendors" "Fails to maintain neat and clean premises", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

	eststo clear

********************************************************************************
* TABLE 2: SUMMARY STATISTICS 												   *
********************************************************************************
* TABLE 2 - Vendor:
	use "$temporales\Bcom_976total_2022.dta",clear
	la var dln_clis "Change, log(number of weekend customers), 2007-17"
	la var edu_nivel "Education"
	la var edad "Age"
	la var fundador "Is a market founder"
	la var nac_fuera "Born outside of Lima and Callao"

	foreach v of glo vendor_controls{
		* Save variable label:
		local l`v' : variable label `v'
		
		* Save N, Mean, S.D, Min, and Max:
		su `v'
		local N`v': di %6.0fc r(N)
		local mean`v': di %5.2fc r(mean)
		local sd`v': di %5.2fc r(sd)
		local min`v': di %5.0fc r(min)
		local max`v': di %5.0gc r(max)
	}
	* Get the sumari stats only for the dependent variable:
		su dln_clis
		local ldln_clis: variable label dln_clis
		local Ndln_clis: di %6.0fc r(N)
		local meandln_clis: di %5.2fc r(mean)
		local sddln_clis: di %5.2fc r(sd)
		local mindln_clis: di %5.2fc r(min)
		local maxdln_clis: di %5.0gc r(max)

* Table 2 - Market:
	use "$temporales\Bmdo_90total_2022.dta", clear
	la var VarBPcomplex "Change in Market Infrastructure Index, 2007–2014"
	la var cump_co "Percent of vendors who always pay ordinary dues"
	la var vecFIN "Social ties among market founders (1-0)"
	la var enf_form "Formal Enforcement"
	la var puestos "Number of stalls in market"
	la var antiguedad "Age of market"
	la var p_funda "Percent of market stalls owned by founders"
	la var p_verdura "\quad \quad Vegetables"
	la var p_fruta "\quad \quad Fruit"
	la var p_carne "\quad \quad Meat"
	la var p_ave "\quad \quad Chicken"
	la var p_pes "\quad \quad Fish"
	la var p_abar "\quad \quad Dried goods"
	la var indmdos17 "Index of number/distance of traditional markets around market (2017)"
	la var indmdos07 "Index of number/distance of traditional markets around market (2007)"
	la var indsuper "Index of number/distance of supermarkets around market"

	foreach v of glo market_vars{
		* Save variable label:
		local l`v' : variable label `v'
		
		* Save N, Mean, S.D, Min, and Max:
		su `v'
		local N`v': di %6.0fc r(N)
		local mean`v': di %5.2fc r(mean)
		local sd`v': di %5.2fc r(sd)
		local min`v': di %5.0fc r(min)
		local max`v': di %7.2gc r(max)
	}
	* Only for dependent variables:
	foreach v of varlist VarBPcomplex cump_co{
		* Save variable label:
		local l`v' : variable label `v'
		
		* Save N, Mean, S.D, Min, and Max:
		su `v'
		local N`v': di %6.0fc r(N)
		local mean`v': di %5.2fc r(mean)
		local sd`v': di %5.2fc r(sd)
		local min`v': di %5.0fc r(min)
		local max`v': di %7.2gc r(max)
	}

	* Generate latex table using texdoc:
		texdoc init "$tablas\tabla2_sumstats.tex", replace force
		tex \begin{tabular}{l*{5}{c}}
		tex \toprule
		tex  & N & Mean & Std. Dev. & Min. & Max. \\ \midrule
		tex \textbf{\textit{Dependent variables}} & & & & \\ \midrule
		
		foreach v in VarBPcomplex cump_co dln_clis{
			tex `l`v'' &  `N`v'' &  `mean`v'' & `sd`v'' & `min`v'' & `max`v'' \\
		}
		tex \midrule
		tex \textbf{\textit{Independent variables – market characteristics}} & & & & \\ \midrule
		
		foreach v of glo market_vars_excl_shares {
			tex `l`v'' &  `N`v'' &  `mean`v'' & `sd`v'' & `min`v'' & `max`v'' \\
		}
		tex Share of stalls in each product category & & & & & \\
		
		foreach v of glo shares{
			tex `l`v'' &  `N`v'' &  `mean`v'' & `sd`v'' & `min`v'' & `max`v'' \\
		}
		tex \midrule
		tex \textbf{\textit{Independent variables – characteristics of respondent vendors}} & & & & \\ \midrule
		
		foreach v of glo vendor_controls {
			tex `l`v'' &  `N`v'' &  `mean`v'' & `sd`v'' & `min`v'' & `max`v'' \\
		}
		
		tex \bottomrule
		tex \end{tabular}
		texdoc close
		
********************************************************************************
* TABLE 3: BALANCE TABLE													   *
********************************************************************************
* TABLE 3 - Market:
	la var Bpcomplex_2007 "Market Infrastructure Index 2007"
	la var socios "Number of vendors with voting rights \textit{(socios)}"
	la var indsuper "Weighted index of supermarket competition in 2017"
	la var enf_formS "Strict Board Enforcement"
	la var enf_soc "\% of respondents who say response to infractions is social pressure"

	foreach	var of varlist  Bpcomplex_2007 socios puestos antiguedad p_verdura p_fruta p_carne p_ave p_pes p_abar  p_funda indsuper indmdos17 enf_form enf_formS enf_soc  {
		*Save variable label:
		local l`var' : variable label `var'
		
		*Save the mean of weak social ties, strong social ties, the difference between the two and the p-value of the difference:
		qui	ttest `var' , by(vecFIN)
		local mu_weak_`var': di %12.3f r(mu_1)
		local mu_strg_`var': di %12.3f r(mu_2)
		local diff=r(mu_1)-r(mu_2)
		local N_weak_`var': di %3.0f  r(N_1) 
		local N_strg_`var': di %3.0f  r(N_2) 
		
		local tt=r(p)
		local tt_fmt_`var': di %8.3f r(p)
		local q="*"
		local qq="**"
		local qqq="***"
		
			if(abs(`tt')<=0.01) {
				local diff_`var': di %12.3f `diff' "`qqq' "
			}
			if(abs(`tt')>0.01 & abs(`tt')<=0.05) {
				local diff_`var': di %12.3f `diff' "`qq' "
			}
			if(abs(`tt')<=0.1 & abs(`tt')>0.05) {
				local diff_`var': di %12.3f `diff' "`q' "
			}
			if(abs(`tt')>0.1) {
				local diff_`var': di %12.3f `diff' 
			}
		
	}

	* Generate latex table using texdoc:
		texdoc init "$tablas\tabla3_balance.tex", replace force
		tex \begin{tabular}{l*{4}{c}}
		tex \toprule
		tex  & Weak social ties & Strong social ties & Difference of means & p-value \\ \midrule
		foreach	var of varlist  Bpcomplex_2007 socios puestos antiguedad p_funda indsuper indmdos17 enf_form enf_formS enf_soc  {
			tex `l`var'' & `mu_weak_`var'' & `mu_strg_`var'' & `diff_`var'' & `tt_fmt_`var'' \\
		}
		tex Share of stalls in each product category & & & & \\
		foreach var of varlist p_verdura p_fruta p_carne p_ave p_pes p_abar{
			tex `l`var'' & `mu_weak_`var'' & `mu_strg_`var'' & `diff_`var'' & `tt_fmt_`var'' \\
		}
		tex \midrule
		tex Number of markets & `N_weak_socios' & `N_strg_socios' & & \\
		tex \bottomrule
		tex \end{tabular}
		texdoc close
		
* End of 1. descriptives.do